#include "board.h"
#include "BLUET.h"
#include "AD.h"
#include "hx711.h"
#include <stdio.h>

//定义控制块
static rt_thread_t FastReg_thread = RT_NULL;
static rt_thread_t period_thread = RT_NULL;


//函数声明
static void FastReg_thread_entry( void* parameter );
static void period_thread_entry( void* parameter );

//master
_Main_t mainPar;
_Sys_t  Sys;
static uint16_t updateTick = 999;//对于部分变量，更新可以慢一些

//HX711变量
int32_t hx711_value;
float weight;
int32_t reset;
float Weights=100.0;  //100g
int32_t Weights_100=8493860;  //100g

// 滑动平均滤波数组和索引
int32_t weight_filter_buffer[FILTER_WINDOW_SIZE];
int filter_index = 0;

//角度变量
uint16_t ADValue;			//定义AD值变量
float Voltage;

// 角度滑动平均滤波数组和索引
uint16_t angle_filter_buffer[ANGLE_FILTER_WINDOW_SIZE];
int angle_filter_index = 0;

void FastModbusUpdate(void)
{
	BLUET_Init(9600);
	AD_Init();
	HX711_Init();
	
	rt_thread_delay(500);
	reset = HX711_GetData();
	
	rt_memset(weight_filter_buffer, 0, sizeof(weight_filter_buffer));
	rt_memset(angle_filter_buffer, 0, sizeof(angle_filter_buffer));
	
	while (1)
  	{	
		//角度
		ADValue = AD_GetValue();
		// Voltage = (float)ADValue / 4095 * 3.3;		//将AD值线性变换到0~3.3的范围，表示电压
		uint16_t filtered_ADValue = angle_sliding_average_filter(ADValue);
		float angle = (ADC_to_angle(filtered_ADValue));
		
		//重量
		hx711_value = HX711_GetData();
		rt_kprintf("%d\r\n", hx711_value);
		int32_t filtered_hx711_value = sliding_average_filter(hx711_value);
		weight = (float)(filtered_hx711_value - reset) * Weights / (float)(Weights_100 - reset);
		// weight = linear_segment_map(filtered_hx711_value - reset);
		rt_kprintf("%d\r\n", filtered_hx711_value - reset);
		rt_kprintf("%d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1));

		Usart3_SendDatas(angle, weight);
		// Usart3_SendDatas(angle, (weight/1000)*9.8);

		rt_thread_delay(100);
	}

}

//处理慢速任务，每秒运行一次
static void period_thread_entry( void* parameter )
{
	// IWDG_Feed_Init(IWDG_Prescaler_256,1000);//约为6秒复位
	//TIM4_Int_Init(40000,719);//用于给LED定时更新使用，
    while ( 1 )
    {
        mainPar.tickSec = rt_tick_get()/1000;//获取时钟
        if(mainPar.tickSec%1 == 0){rt_kprintf("Run Time:%dH-%dM-%dS.\r\n",((mainPar.tickSec/60)/60),
				((mainPar.tickSec/60)%60),mainPar.tickSec%60);	 }	
//        		
			GpioSet(GPIO_MDU_LED,0);
			
			rt_thread_delay( 500 ); /* 延时500个tick */
				
			GpioSet(GPIO_MDU_LED,1);

			rt_thread_delay( 500 ); /* 延时500个tick */
        
			//rt_kprintf("tim3tick:%d\r\n",mainPar.timtick);
			mainPar.timtick = 0;
			
			//更新周期
			//函数调用周期为1000ms
			updateTick ++;
			if(updateTick > 1000){updateTick = 0;}		
   }
}

//快速或重要寄存器更新
//延迟后会产生影响的寄存器
static void FastReg_thread_entry( void* parameter )
{

    while ( 1 )
    {
		IWDG_Feed();			
		//寄存器更新
		FastModbusUpdate();
      rt_thread_delay( 100 );
		
    }
}

int main( void )
{  
	FastReg_thread =                        
        rt_thread_create( "FastReg",             
                          FastReg_thread_entry,  
                          RT_NULL,           
                          2048,                 
                          3,                   
                          20 );
    period_thread =                        
        rt_thread_create( "period",             
                          period_thread_entry,  
                          RT_NULL,           
                          4096,                 
                          4,                   
                          20 );  	

//参数初始化
    Sys.modbusAdd = 0x01;  		//设置本设备的地址

	if(FastReg_thread != RT_NULL)
         rt_thread_startup( FastReg_thread );
    if ( period_thread != RT_NULL )
        rt_thread_startup( period_thread );
	return -1;

}


